package com.wan37.logic.email.serivce.impl;

import com.wan37.logic.email.dao.AttachmentDao;
import com.wan37.logic.email.dao.EmailDao;
import com.wan37.logic.email.domain.entity.AttachmentDO;
import com.wan37.logic.email.domain.entity.EmailDO;
import com.wan37.logic.email.domain.vo.AttachmentVO;
import com.wan37.logic.email.domain.vo.EmailVO;
import com.wan37.logic.email.serivce.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-28 11:15
 **/
@Service("emailService")
public class EmailServiceImpl implements EmailService {
    @Autowired
    private EmailDao emailDao;
    @Autowired
    private AttachmentDao attachmentDao;
    @Override
    public boolean update(EmailVO emailVO) {
        emailDao.update(new EmailDO(emailVO));
        return true;
    }

    @Override
    public boolean save(EmailVO emailVO) {
        return false;
    }

    @Override
    public boolean delete(EmailVO emailVO) {
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveEmailAndAttachment(EmailVO emailVO) {
        try {
            emailDao.add(new EmailDO(emailVO));
            if (emailVO.getAttachmentVOList() != null){
                for (AttachmentVO attachmentVO : emailVO.getAttachmentVOList()){
                    attachmentDao.add(new AttachmentDO(attachmentVO));
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean saveEmailAndAttachment(List<EmailVO> emailVOList) {
        try {
            List<AttachmentVO> attachmentVOList = null;
            boolean flag = true;
            for (EmailVO emailVO : emailVOList){
                emailDao.add(new EmailDO(emailVO));
                if (flag){
                    attachmentVOList = emailVO.getAttachmentVOList();
                    flag = false;
                }
            }
            if (attachmentVOList != null){
                for (AttachmentVO attachmentVO : attachmentVOList){
                    attachmentDao.add(new AttachmentDO(attachmentVO));
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<EmailVO> findEmailVOByUserId(String userId) {
        List<EmailDO> emailDOList = emailDao.getEmailDOAndReceiverId(userId);
        List<EmailVO> emailVOList = null;
        List<AttachmentDO> attachmentDOList = new ArrayList<>();
        if (emailDOList != null){
            emailVOList = new ArrayList<>();
            for (EmailDO emailDO : emailDOList){
                emailVOList.add(new EmailVO(emailDO));
                List<AttachmentDO> attachmentDOList1 = attachmentDao.getAttachmentDOByEmailId(emailDO.getEmailId());
                attachmentDOList.addAll(attachmentDOList1);
            }
            if (attachmentDOList != null){
                for (AttachmentDO attachmentDO : attachmentDOList){
                    for (EmailVO emailVO : emailVOList){
                        if (attachmentDO.getEmailId().equals(emailVO.getEmailId())){
                            if (emailVO.getAttachmentVOList() == null){
                                emailVO.setAttachmentVOList(new ArrayList<>());
                            }
                            emailVO.getAttachmentVOList().add(new AttachmentVO(attachmentDO));
                            break;
                        }
                    }
                }
            }
        }
        return emailVOList;
    }
}
